package utils

import (
	"fmt"
	nanoid "github.com/matoous/go-nanoid/v2"
	"math/rand"
	"time"
)

// NewNanoid 生成Nanoid字符串
func NewNanoid(num int) string {
	chars := "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
	if id, err := nanoid.Generate(chars, num); err == nil {
		return id
	}
	return ""
}

// NewOrdNum 生成23位订单号
func NewOrdNum() string {
	// 获取当前时间格式的字符串(14位)
	ordNum := time.Now().Format("20060102150405")
	// 生成8位随机数
	randNum := fmt.Sprintf("%08v", rand.Intn(100000000))
	return "D" + ordNum + randNum
}

// RandomStr 生成N位的随机数
func RandomStr(n int) string {
	letters := "abcdefghijklmnopqrstuvwxyz0123456789"
	retStr := make([]byte, n)
	rand.New(rand.NewSource(time.Now().Unix()))
	for i := range retStr {
		retStr[i] = letters[rand.Intn(len(letters))]
	}
	return string(retStr)
}
